<?php
namespace App\Models;

class SysMenu extends Base {

    public function initialize() {
        parent::initialize();
    }

    public function getSource() {
        return "sys_menu";
    }
    /**
     * 添加菜单信息
     *
     * @param array $entity 菜单信息
     *
     * @return array|false
     */
    public function add($entity) {
        try {
            if($this->db()->insertAsDict($this->getSource(), $entity)) {
                return $this->db()->lastInsertId();
            }
        } catch(\PDOException $ex) {
            $this->errorLog('INSERT', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    /**
     * 修改菜单信息
     *
     * @param array $entity 菜单信息
     *
     * @return array|false
     */
    public function edit($entity) {
        try {
            return $this->db()->updateAsDict($this->getSource(), $entity, sprintf("id=%d LIMIT 1", intval($entity['id'])));
        } catch(\PDOException $ex) {
            $this->errorLog('UPDATE', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
            return false;
        }
    }

    /**
     * 获取所有菜单信息
     *
     * @return array|false
     */
    public function getMenus() {
        $query = "SELECT s1.*,s2.`name` AS up_name FROM `sys_menu` AS s1 LEFT JOIN `sys_menu` AS s2 ON s1.up_id = s2.id  ORDER BY  `priority` DESC";
        try {
            $ret = $this->db()->query($query)->fetchAll();
            if($ret !== false && is_array($ret)) {
                return $ret;
            }
        } catch(\PDOException $ex) {
            $this->errorLog('SELECT', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    /**
     * 根据菜单id值来获取菜单信息
     *
     * @param int $id 菜单id值
     *
     * @return array|false
     */
    public function getMenu($id) {
        $query = 'SELECT * FROM `sys_menu` WHERE `id` = :id LIMIT 1';
        try {
            $ret = $this->db()->query($query,array('id' => $id))->fetch();
            if($ret !== false && is_array($ret)) {
                return $ret;
            }
        } catch(\PDOException $ex) {
            $this->errorLog('SELECT', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    /**
     * 获取所有的顶级菜单信息
     *
     * @return array|false
     */
    public function getTops(){
        $query = "SELECT `id`,`name` FROM `sys_menu` WHERE `up_id`=0 ORDER BY `priority` DESC";
        try {
            $ret = $this->db()->query($query)->fetchAll();
            if($ret !== false && is_array($ret)) {
                return $ret;
            }
        } catch(\PDOException $ex) {
            $this->errorLog('SELECT', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    public function getRoleMenus() {
        $ret = $this->db()->query('SELECT `id`, `name`,`url`,`icon`,`up_id` FROM `sys_menu` ORDER BY `up_id` ASC,`priority` DESC, `id` ASC')->fetchAll();
        if($ret !== false && is_array($ret)) {
            $menus = array();
            foreach($ret as $v) {
                $menus[$v['id']] = $v;
            }
            return $menus;
        }
        return false;
    }
    public function gets() {
        try {
            $ret = $this->db()->query('SELECT * FROM `sys_menu`')->fetchAll();
            if($ret !== false && is_array($ret)) {
                $out = [];
                foreach($ret as $v) {
                    $out[$v['id']] = $v;
                }
                unset($ret);
                return $out;
            }
        } catch(\PDOException $ex) {
            $this->errorLog('SELECT', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }
}
